<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
class Document_Model extends CI_Model {
    //***************/
    //  RETRIEVAL
    //***************/
    
    
    
    function get_last_iddeliveryorder_by_idshippingline($idShippingLine){
        //get last index article
        $this->db->select('idDeliveryOrder');
        $this->db->order_by('idDeliveryOrder', 'DESC');
        $this->db->where('idShippingLine',$idShippingLine);
        $this->db->limit(1);
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->row(0)->idDeliveryOrder;
        }else{
            return false;
        }
    }
    function get_deliveryorder_by_iddeliveryorder($idDeliveryOrder){
        //get last index article
        $this->db->select('deliveryorder.*, consignee.Name as cname, shippingline.Name as slname');
        $this->db->where('deliveryorder.idDeliveryOrder',$idDeliveryOrder);
        $this->db->join('consignee', 'consignee.idConsignee = deliveryorder.idConsignee');
        $this->db->join('shippingline', 'shippingline.idShippingline = deliveryorder.idShippingLine');
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            $datas = array();
            $datas['deliveryorder'] = $result->result();
            $datas['lineitem'] = $this->get_deliveryorder_lineitem_by_iddeliveryorder($idDeliveryOrder);
            //print_r($data);
            return $datas;
        }else{
            return false;
        }
    }
    function get_iddeliveryorder_by_idconsignee($idConsignee){
        $this->db->select('idDeliveryOrder');
        $this->db->where('idConsignee',$idConsignee);
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_deliveryorder_by_idshippingline($idShippingLine){
        $this->db->select('deliveryorder.*, consignee.Name');
        $this->db->order_by('deliveryorder.idDeliveryOrder', 'DESC');
        $this->db->where('idShippingLine',$idShippingLine);
        $this->db->join('consignee', 'consignee.idConsignee = deliveryorder.idConsignee');
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_deliveryorder_by_idconsignee($idConsignee){
        //get last index article
        $this->db->select('deliveryorder.*, shippingline.Name');
        $this->db->order_by('deliveryorder.idDeliveryOrder', 'DESC');
        $this->db->where('idConsignee',$idConsignee);
        $this->db->join('shippingline', 'shippingline.idShippingline = deliveryorder.idShippingLine');
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    
    function get_deliveryorder_notification_by_idconsignee($idConsignee){
        $dataibl = $this->get_iddeliveryorder_by_idconsignee($idConsignee);
        if($dataibl){
            $arrayibl = array();
            foreach($dataibl as $row){
                $idbl = $row->idDeliveryOrder;
                array_push($arrayibl,$idbl);
            }
            $this->db->select('deliveryordernotification.* , shippingline.Name, deliveryorder.*');
            $this->db->where_in('deliveryordernotification.idDeliveryOrder',$arrayibl);
            $this->db->where('Action','send');
            $this->db->where('ReadStatus',false);
            $this->db->join('deliveryorder', 'deliveryorder.idDeliveryOrder = deliveryordernotification.idDeliveryOrder');
            $this->db->join('shippingline', 'shippingline.idShippingline = deliveryorder.idShippingLine');
            $result = $this->db->get('deliveryordernotification');
            if($result->num_rows>0){
                return $result->result();
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    function get_iddeliveryorder_by_idshippingline($idshippingline){
        $this->db->select('idDeliveryOrder');
        $this->db->where('idShippingLine',$idshippingline);
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_deliveryorder_notification_by_idshippingline($idshippingline){
        $dataibl = $this->get_iddeliveryorder_by_idshippingline($idshippingline);
        if($dataibl){
            $arrayibl = array();
            foreach($dataibl as $row){
                $idbl = $row->idDeliveryOrder;
                array_push($arrayibl,$idbl);
            }
            $this->db->select('deliveryordernotification.* , consignee.Name, deliveryorder.*');
            $this->db->where_in('deliveryordernotification.idDeliveryOrder',$arrayibl);
            $this->db->where('Action','reply');
            $this->db->where('ReadStatus',false);
            $this->db->join('deliveryorder', 'deliveryorder.idDeliveryOrder = deliveryordernotification.idDeliveryOrder');
            $this->db->join('consignee', 'consignee.idConsignee = deliveryorder.idConsignee');
            $result = $this->db->get('deliveryordernotification');
            if($result->num_rows>0){
                return $result->result();
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    function get_deliveryorder_lineitem_by_iddeliveryorder($iddeliveryorder){
        $this->db->where('iddeliveryorder',$iddeliveryorder);
        $result = $this->db->get('deliveryorderlineitem');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_deliveryorder_filename_by_iddeliveryorder($iddeliveryorder){
        $this->db->select('LocationFile');
        $this->db->where('idDeliveryOrder',$iddeliveryorder);
        $result = $this->db->get('deliveryorder');
        if($result->num_rows>0){
            return $result->row(0)->LocationFile;
        }else{
            return false;
        }
    }
    //***************/
    //  UPDATE
    //***************/
    function update_deliveryorder_notification($iddeliveryorder, $action, $readstatus){
        $data = array(
               'ReadStatus' => $readstatus
            );
        $this->db->where('idDeliveryOrder', $iddeliveryorder);
        $this->db->where('Action', $action);
        return $this->db->update('deliveryordernotification', $data); 
    }
    function update_status_deliveryorder($iddeliveryorder,$data){
        $this->db->where('idDeliveryOrder', $iddeliveryorder);
        return $this->db->update('deliveryorder', $data); 
    }
    //***************/
    //  INSERT
    //***************/
    function insert_freightinvoice($FI){
        //$this->db->insert('freightinvoice', $FI['data']);
        //$
        //foreach($FI['lineitem'] as $lineitem){
        //    $fili = $line
        //}
    }
    
    function insert_freightinvoice_lineitem($FILI){
        return $this->db->insert('freightinvoice_lineitem', $FILI);
    }
    
    function insert_freightinvoice_lineitem_containerdetail($FILICD){
        return $this->db->insert('freightinvoice_lineitem_containerdetail', $FILICD);
    }
    
    function insert_deliveryorder($DO){
        return $this->db->insert('deliveryorder', $DO);
    }
    
    function insert_deliveryorder_lineitem($DOLI){
        return $this->db->insert('deliveryorder_lineitem', $DOLI);
    }
    
    function insert_deliveryorder_lineitem_containerdetail($DOLICD){
        return $this->db->insert('deliveryorder_lineitem_containerdetail', $DOLICD);
    }
    
}
